<div class="row">
    <div class="col-md-12">
        <button id="addCustomer" class="btn btn-info">Thêm khách hàng</button>
    </div>
    <br />
</div>
<div class="row">
    <div class="col-md-12">
        <div class="box-widget">
            <table id="list-group" class="table data-tbl-boxy responsive">
                <thead>
                    <tr>
                        <th>
                            <?php echo $this->translate('customer name'); ?>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($this->data as $row): ?>
                    <tr>
                        <td>
                            <a href="javascript:edit('<?php echo $row->id; ?>')">
                                <?php echo $row->name; ?>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div id="add-customer" title="Thêm khách hàng">
    <form id="add-customer-form">
        Tên khách hàng<br />
        <input type="text" name="name" id="name" /><br />
        Điện thoại<br />
        <input type="text" value="" name="phone_number" id="phone_number" /><br />
        Địa chỉ<br />
        <input type="text" value="" name="address" id="address" />
        <input type="hidden" value="0" name="discount_type" id="discount_type" />
        <input type="hidden" value="0" name="discount_value" id="discount_value" />
        <input type="hidden" name="type" id="type" value="0" /> 
    </form>
</div>
<script>
function edit(id){
    var form = '<form action="<?php echo $this->url(array('module' => 'default', 'controller' => 'sales-invoice', 'action' => $this->action), null, true); ?>" method="post" id="edit-user-' + id + '">';
        form += '<input type="hidden" name="customer_id" value="' + id + '" />';
        form += '</form>';
    $("table").append(form);
    $("#edit-user-" + id).submit();
}

$(document).ready(function(){
    var dialogCustomer = $("div#add-customer").dialog({
        autoOpen: false,
        height: 350,
        width: 300,
        modal: true,
        buttons: {
            "Thêm": addCustomerAndSupplier,
            Cancel: function(){
                dialogCustomer.dialog("close");
            }
        },
        close: function(){
            $("form#add-customer-form").trigger("reset");
        }
    });
    
    function addCustomerAndSupplier(){
        var type = $("form#add-customer-form input#type").val();
        url = "<?php echo $this->url(array('module' => 'default', 'controller' => 'customer', 'action' => 'create-ajax'), null, true) ?>";
        
        var name = $("form#add-customer-form input#name").val();
        var phone_number = $("form#add-customer-form input#phone_number").val();
        var address = $("form#add-customer-form input#address").val();
        
        $.ajax({
            url: url,
            data: {'name': name, 'phone_number': phone_number, 'address': address, 'discount_type': 0, 'discount_value': 0},
            success: function (result){
                window.location.reload();
            }
        });
    }
    
    $("button#addCustomer").click(function(){
        dialogCustomer.dialog("open");
    });
});

$(function() {
    $('.data-tbl-boxy').dataTable({
        "sPaginationType": "bootstrap",
        "iDisplayLength": 10,
        "oLanguage": {
            "sLengthMenu": "<span class='lengthLabel pull-left'><?php echo $this->translate('entries_per_page'); ?>:</span><span class='lenghtMenu pull-left'> _MENU_</span>",
            "sInfo": "<?php echo $this->translate('show start end records of total'); ?>",
            "sSearch": "<?php echo $this->translate('filter'); ?>:",
            "oPaginate": {
                "sFirst": "<?php echo $this->translate('first page'); ?>",
                "sLast": "<?php echo $this->translate('last page'); ?>",
                "sNext": "<?php echo $this->translate('next page'); ?>",
                "sPrevious": "<?php echo $this->translate('previous page'); ?>"
            }
        },
        "sDom": '<"widget-head clearfix"fl>,<"widget-container"<"widget-block"<"widget-content"t>,,<"table-bottom clearfix"<"tbl-pagination pull-left"p><"tbl-data-info pull-right"i>>'
    });
});
</script>